<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="baidu-site-verification" content="code-Qk3hgFvTdc">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"gitee.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":true,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":true},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"disqus","storage":true,"lazyload":false,"nav":{"disqus":{"text":"Load Disqus","order":-1}}},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="Xss 安全过滤插件XSS 是什么XSS（Cross Site Scripting）攻击全称跨站脚本攻击，为了不与 CSS(Cascading Style Sheets)名词混淆，故将跨站脚本攻击简称为 XSS，XSS 是一种常见 web 安全漏洞，它允许恶意代码植入到提供给其它用户使用的页面中。">
<meta property="og:type" content="article">
<meta property="og:title" content="Xss 安全过滤">
<meta property="og:url" content="https://gitee.com/feege/feege.git/2021/08/17/Xss%E5%AE%89%E5%85%A8%E8%BF%87%E6%BB%A4/index.html">
<meta property="og:site_name" content="云记">
<meta property="og:description" content="Xss 安全过滤插件XSS 是什么XSS（Cross Site Scripting）攻击全称跨站脚本攻击，为了不与 CSS(Cascading Style Sheets)名词混淆，故将跨站脚本攻击简称为 XSS，XSS 是一种常见 web 安全漏洞，它允许恶意代码植入到提供给其它用户使用的页面中。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/images/Xss%E5%AE%89%E5%85%A8%E8%BF%87%E6%BB%A4/20201201165055.png">
<meta property="og:image" content="https://gitee.com/images/Xss%E5%AE%89%E5%85%A8%E8%BF%87%E6%BB%A4/20201201170642.png">
<meta property="og:image" content="https://gitee.com/images/Xss%E5%AE%89%E5%85%A8%E8%BF%87%E6%BB%A4/20201201202924.png">
<meta property="og:image" content="https://gitee.com/images/Xss%E5%AE%89%E5%85%A8%E8%BF%87%E6%BB%A4/20201201202352.png">
<meta property="og:image" content="https://gitee.com/images/Xss%E5%AE%89%E5%85%A8%E8%BF%87%E6%BB%A4/20201201201251.png">
<meta property="og:image" content="https://gitee.com/images/Xss%E5%AE%89%E5%85%A8%E8%BF%87%E6%BB%A4/20201201201611.png">
<meta property="article:published_time" content="2021-08-17T07:56:11.487Z">
<meta property="article:modified_time" content="2021-08-17T08:00:02.604Z">
<meta property="article:author" content="云记">
<meta property="article:tag" content="Java,面试,offer,技术,微服务,分布式,中间件,数据库,bat">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/images/Xss%E5%AE%89%E5%85%A8%E8%BF%87%E6%BB%A4/20201201165055.png">

<link rel="canonical" href="https://gitee.com/feege/feege.git/2021/08/17/Xss%E5%AE%89%E5%85%A8%E8%BF%87%E6%BB%A4/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Xss 安全过滤 | 云记</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">云记</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">云记</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">5</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">12</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/feege/feege.git/2021/08/17/Xss%E5%AE%89%E5%85%A8%E8%BF%87%E6%BB%A4/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="云记">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="云记">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Xss 安全过滤
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-08-17 15:56:11" itemprop="dateCreated datePublished" datetime="2021-08-17T15:56:11+08:00">2021-08-17</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%88%86%E5%B8%83%E5%BC%8F-%E5%BE%AE%E6%9C%8D%E5%8A%A1/" itemprop="url" rel="index"><span itemprop="name">分布式/微服务</span></a>
                </span>
            </span>

          
            <span id="/2021/08/17/Xss%E5%AE%89%E5%85%A8%E8%BF%87%E6%BB%A4/" class="post-meta-item leancloud_visitors" data-flag-title="Xss 安全过滤" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/08/17/Xss%E5%AE%89%E5%85%A8%E8%BF%87%E6%BB%A4/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/08/17/Xss%E5%AE%89%E5%85%A8%E8%BF%87%E6%BB%A4/" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>2.8k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>3 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="Xss-安全过滤插件"><a href="#Xss-安全过滤插件" class="headerlink" title="Xss 安全过滤插件"></a>Xss 安全过滤插件</h1><h1 id="XSS-是什么"><a href="#XSS-是什么" class="headerlink" title="XSS 是什么"></a>XSS 是什么</h1><p>XSS（Cross Site Scripting）攻击全称跨站脚本攻击，为了不与 CSS(Cascading Style Sheets)名词混淆，故将跨站脚本攻击简称为 XSS，XSS 是一种常见 web 安全漏洞，它允许恶意代码植入到提供给其它用户使用的页面中。</p>
<span id="more"></span>



<h2 id="xss-攻击流程"><a href="#xss-攻击流程" class="headerlink" title="xss 攻击流程"></a>xss 攻击流程</h2><p><img src="/images/Xss%E5%AE%89%E5%85%A8%E8%BF%87%E6%BB%A4/20201201165055.png" alt="img"></p>
<h2 id="简单-xss-攻击示例"><a href="#简单-xss-攻击示例" class="headerlink" title="简单 xss 攻击示例"></a>简单 xss 攻击示例</h2><ul>
<li>若网站某个表单没做相关的处理，用户提交相关恶意代码，浏览器会执行相关的代码。</li>
</ul>
<p><img src="/images/Xss%E5%AE%89%E5%85%A8%E8%BF%87%E6%BB%A4/20201201170642.png" alt="img"></p>
<h1 id="解决方案"><a href="#解决方案" class="headerlink" title="解决方案"></a>解决方案</h1><h2 id="XSS-过滤说明"><a href="#XSS-过滤说明" class="headerlink" title="XSS 过滤说明"></a>XSS 过滤说明</h2><ul>
<li><p>对表单绑定的字符串类型进行 xss 处理。</p>
</li>
<li><p>对 json 字符串数据进行 xss 处理。</p>
</li>
<li><p>提供路由和控制器方法级别的放行规则。</p>
</li>
</ul>
<h2 id="使用-mica-xss"><a href="#使用-mica-xss" class="headerlink" title="使用 mica-xss"></a>使用 mica-xss</h2><p>引入一下 依赖即可</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--XSS 安全过滤--&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>net.dreamlu<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mica-core<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">version</span>&gt;</span>2.5.0.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>net.dreamlu<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mica-xss<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">version</span>&gt;</span>2.5.0.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h2 id="测试-XSS-过滤"><a href="#测试-XSS-过滤" class="headerlink" title="测试 XSS 过滤"></a>测试 XSS 过滤</h2><h3 id="测试-GET-参数过滤"><a href="#测试-GET-参数过滤" class="headerlink" title="测试 GET 参数过滤"></a>测试 GET 参数过滤</h3><ul>
<li>创建目标接口，模拟 get 提交</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@GetMapping(&quot;/xss&quot;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">xss</span><span class="params">(String params)</span></span>&#123;</span><br><span class="line">  <span class="keyword">return</span> params;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<ul>
<li>返回为空</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">⋊&gt; ~ curl --location --request GET &#x27;http://localhost:8080/xss?params=%3Cscript%3Ealert(%27xxx%27)%3C/script%3E&#x27;</span><br></pre></td></tr></table></figure>



<h3 id="测试-POST-form-参数过滤"><a href="#测试-POST-form-参数过滤" class="headerlink" title="测试 POST form 参数过滤"></a>测试 POST form 参数过滤</h3><ul>
<li>创建目标接口，模拟 post form 提交</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@PostMapping(&quot;/xss&quot;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">xss</span><span class="params">(String params)</span></span>&#123;</span><br><span class="line">  <span class="keyword">return</span> params;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<ul>
<li>返回为空</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">curl --location --request POST &#x27;http://localhost:8080/xss&#x27; \</span><br><span class="line">--header &#x27;Content-Type: application/x-www-form-urlencoded&#x27; \</span><br><span class="line">--data-urlencode &#x27;params=&lt;script&gt;alert(&#x27;\&#x27;&#x27;xxx&#x27;\&#x27;&#x27;)&lt;/script&gt;&#x27;</span><br></pre></td></tr></table></figure>



<h3 id="测试-POST-body-参数过滤"><a href="#测试-POST-body-参数过滤" class="headerlink" title="测试 POST body 参数过滤"></a>测试 POST body 参数过滤</h3><ul>
<li>创建目标接口，模拟 post body 提交</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@PostMapping(&quot;/xss&quot;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">xss</span><span class="params">(<span class="meta">@RequestBody</span> Map&lt;String,String&gt; body)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> body.get(<span class="string">&quot;params&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<ul>
<li>返回为空</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">curl --location --request POST &#x27;http://localhost:8080/xss&#x27; \</span><br><span class="line">--header &#x27;Content-Type: application/json&#x27; \</span><br><span class="line">--data-raw &#x27;&#123;</span><br><span class="line">    &quot;params&quot;:&quot;&lt;script&gt;alert(&#x27;\&#x27;&#x27;XXX&#x27;\&#x27;&#x27;)&lt;/script&gt;&quot;</span><br><span class="line">&#125;&#x27;</span><br></pre></td></tr></table></figure>



<h3 id="跳过某些接口过滤"><a href="#跳过某些接口过滤" class="headerlink" title="跳过某些接口过滤"></a>跳过某些接口过滤</h3><p>可以使用 <a href><strong>@XssCleanIgnore</strong> </a>  注解对方法和类级别进行忽略。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">@XssCleanIgnore</span><br><span class="line">@PostMapping(&quot;/xss&quot;)</span><br><span class="line">public String xss(@RequestBody Map&lt;String,String&gt; body)&#123;</span><br><span class="line">  return body.get(&quot;params&quot;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="原理分析"><a href="#原理分析" class="headerlink" title="原理分析"></a>原理分析</h2><h3 id="常见实现剖析"><a href="#常见实现剖析" class="headerlink" title="常见实现剖析"></a>常见实现剖析</h3><ul>
<li>目前网上大多数的方案如下图，新增 XssFilter 拦截用户提交的参数，进行相关的转义和黑名单排除，完成相关的业务逻辑。<strong>在整个过程中最核心的是通过包装用户的原始请求，创建新的 requestwrapper 保证请求流在后边的流程可以重复读</strong>。</li>
</ul>
<p><img src="/images/Xss%E5%AE%89%E5%85%A8%E8%BF%87%E6%BB%A4/20201201202924.png" alt="img"></p>
<h2 id="mica-xss-实现"><a href="#mica-xss-实现" class="headerlink" title="mica-xss 实现"></a>mica-xss 实现</h2><h3 id="1-自定义-WebDataBinder-编辑器支持-form-过滤"><a href="#1-自定义-WebDataBinder-编辑器支持-form-过滤" class="headerlink" title="1. 自定义 WebDataBinder 编辑器支持 form 过滤"></a>1. 自定义 WebDataBinder 编辑器支持 form 过滤</h3><p>Spring WebDataBinder 的作用是从 web request 中把 web 请求里的<code>parameters</code>绑定到对应的<code>JavaBean</code>上，在 Controller 方法中的参数类型可以是基本类型，也可以是封装后的普通 Java 类型。若这个普通的 Java 类型没有声明任何注解，则意味着它的每一个属性都需要到 Request 中去查找对应的请求参数，而 WebDataBinder 则可以帮助我们实现从 Request 中取出请求参数并绑定到 JavaBean 中。SpringMVC 在绑定的过程中提供了用户自定义编辑绑定的接口，注入即可在参数绑定 JavaBean 过程中执行过滤。</p>
<p><img src="/images/Xss%E5%AE%89%E5%85%A8%E8%BF%87%E6%BB%A4/20201201202352.png" alt="img"></p>
<h3 id="2-自定义-JsonDeserializer-反序列化支持-Json-过滤"><a href="#2-自定义-JsonDeserializer-反序列化支持-Json-过滤" class="headerlink" title="2. 自定义 JsonDeserializer 反序列化支持 Json 过滤"></a>2. 自定义 JsonDeserializer 反序列化支持 Json 过滤</h3><p>在 Spring Boot 中默认是使用 Jackson 进行序列化和反序列化 JSON 数据的，那么除了可以用默认的之外，我们也可以编写自己的 JsonSerializer 和 JsonDeserializer 类，来进行自定义操作。用户提交 JSON 报文会通过 Jackson 的 JsonDeserializer 绑定到 JavaBean 中。我们只需要自定义 JsonDeserializer 即可完成在绑定 JavaBean 中执行过滤。</p>
<p><img src="/images/Xss%E5%AE%89%E5%85%A8%E8%BF%87%E6%BB%A4/20201201201251.png" alt="img"></p>
<h3 id="3-核心过滤逻辑"><a href="#3-核心过滤逻辑" class="headerlink" title="3.核心过滤逻辑"></a>3.核心过滤逻辑</h3><p>在 mica-xss 中并未采取上文所述通过自己手写黑名单或者转义方式的实现方案，而是直接实现 Jsoup 这个工具类。</p>
<p>jsoup 实现 WHATWG HTML5 规范，并将 HTML 解析为与现代浏览器相同的 DOM。</p>
<ul>
<li><p>从 URL，文件或字符串中刮取和解析 HTML</p>
</li>
<li><p>使用 DOM 遍历或 CSS 选择器查找和提取数据</p>
</li>
<li><p>操纵 HTML 元素，属性和文本</p>
</li>
<li><p>清除用户提交的内容以防止安全白名单，以防止 XSS 攻击</p>
</li>
<li><p>输出整洁的 HTML</p>
</li>
</ul>
<p><img src="/images/Xss%E5%AE%89%E5%85%A8%E8%BF%87%E6%BB%A4/20201201201611.png" alt="img"></p>

    </div>

    
    
    
        <div class="reward-container">
  <div></div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/wechatpay.png" alt="云记 微信支付">
        <p>微信支付</p>
      </div>
      
      <div style="display: inline-block;">
        <img src="/images/alipay.png" alt="云记 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>


      <footer class="post-footer">

        
  <div class="post-widgets">
    <div class="wp_rating">
      <div id="wpac-rating"></div>
    </div>
  </div>


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/08/12/%E5%A6%82%E4%BD%95%E4%BF%9D%E8%AF%81Kafka%E7%9A%84%E6%B6%88%E6%81%AF%E5%8F%AF%E9%9D%A0%E6%80%A7/" rel="prev" title="如何保证Kafka的消息可靠性">
      <i class="fa fa-chevron-left"></i> 如何保证Kafka的消息可靠性
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/08/18/Java%E6%9C%80%E5%85%A8%E9%9D%A2%E8%AF%95%E9%A2%98/" rel="next" title="史上最全Java面试题(陆续更新中...)">
      史上最全Java面试题(陆续更新中...) <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Xss-%E5%AE%89%E5%85%A8%E8%BF%87%E6%BB%A4%E6%8F%92%E4%BB%B6"><span class="nav-number">1.</span> <span class="nav-text">Xss 安全过滤插件</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#XSS-%E6%98%AF%E4%BB%80%E4%B9%88"><span class="nav-number">2.</span> <span class="nav-text">XSS 是什么</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#xss-%E6%94%BB%E5%87%BB%E6%B5%81%E7%A8%8B"><span class="nav-number">2.1.</span> <span class="nav-text">xss 攻击流程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%AE%80%E5%8D%95-xss-%E6%94%BB%E5%87%BB%E7%A4%BA%E4%BE%8B"><span class="nav-number">2.2.</span> <span class="nav-text">简单 xss 攻击示例</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88"><span class="nav-number">3.</span> <span class="nav-text">解决方案</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#XSS-%E8%BF%87%E6%BB%A4%E8%AF%B4%E6%98%8E"><span class="nav-number">3.1.</span> <span class="nav-text">XSS 过滤说明</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8-mica-xss"><span class="nav-number">3.2.</span> <span class="nav-text">使用 mica-xss</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B5%8B%E8%AF%95-XSS-%E8%BF%87%E6%BB%A4"><span class="nav-number">3.3.</span> <span class="nav-text">测试 XSS 过滤</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B5%8B%E8%AF%95-GET-%E5%8F%82%E6%95%B0%E8%BF%87%E6%BB%A4"><span class="nav-number">3.3.1.</span> <span class="nav-text">测试 GET 参数过滤</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B5%8B%E8%AF%95-POST-form-%E5%8F%82%E6%95%B0%E8%BF%87%E6%BB%A4"><span class="nav-number">3.3.2.</span> <span class="nav-text">测试 POST form 参数过滤</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B5%8B%E8%AF%95-POST-body-%E5%8F%82%E6%95%B0%E8%BF%87%E6%BB%A4"><span class="nav-number">3.3.3.</span> <span class="nav-text">测试 POST body 参数过滤</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%B7%B3%E8%BF%87%E6%9F%90%E4%BA%9B%E6%8E%A5%E5%8F%A3%E8%BF%87%E6%BB%A4"><span class="nav-number">3.3.4.</span> <span class="nav-text">跳过某些接口过滤</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90"><span class="nav-number">3.4.</span> <span class="nav-text">原理分析</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B8%B8%E8%A7%81%E5%AE%9E%E7%8E%B0%E5%89%96%E6%9E%90"><span class="nav-number">3.4.1.</span> <span class="nav-text">常见实现剖析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#mica-xss-%E5%AE%9E%E7%8E%B0"><span class="nav-number">3.5.</span> <span class="nav-text">mica-xss 实现</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E8%87%AA%E5%AE%9A%E4%B9%89-WebDataBinder-%E7%BC%96%E8%BE%91%E5%99%A8%E6%94%AF%E6%8C%81-form-%E8%BF%87%E6%BB%A4"><span class="nav-number">3.5.1.</span> <span class="nav-text">1. 自定义 WebDataBinder 编辑器支持 form 过滤</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E8%87%AA%E5%AE%9A%E4%B9%89-JsonDeserializer-%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E6%94%AF%E6%8C%81-Json-%E8%BF%87%E6%BB%A4"><span class="nav-number">3.5.2.</span> <span class="nav-text">2. 自定义 JsonDeserializer 反序列化支持 Json 过滤</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E6%A0%B8%E5%BF%83%E8%BF%87%E6%BB%A4%E9%80%BB%E8%BE%91"><span class="nav-number">3.5.3.</span> <span class="nav-text">3.核心过滤逻辑</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">云记</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">12</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">5</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://gitee.com/feege" title="GitHub → https:&#x2F;&#x2F;gitee.com&#x2F;feege"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">云记</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">217k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">3:18</span>
</div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>




  
  <script>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>



  <script>
  if (CONFIG.page.isPost) {
    wpac_init = window.wpac_init || [];
    wpac_init.push({
      widget: 'Rating',
      id    : ,
      el    : 'wpac-rating',
      color : 'fc6423'
    });
    (function() {
      if ('WIDGETPACK_LOADED' in window) return;
      WIDGETPACK_LOADED = true;
      var mc = document.createElement('script');
      mc.type = 'text/javascript';
      mc.async = true;
      mc.src = '//embed.widgetpack.com/widget.js';
      var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(mc, s.nextSibling);
    })();
  }
  </script>

  
<script src="/js/local-search.js"></script>











<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : false,
      appId      : 'QCF1KJ4ILmj73nVybNhgiHEJ-gzGzoHsz',
      appKey     : 'di34H8f5jhsb77nm4gtqtxQu',
      placeholder: "发表下你的看法!",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : true,
      lang       : 'zh-cn' || 'zh-cn',
      path       : location.pathname,
      recordIP   : true,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
